package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.CollectGoods;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;
import com.wholesale.utils.FavoriteListAdapter;

public class MyFavoritesLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private RelativeLayout loadingLayout;
	
	private FavoriteListAdapter listAdapter ;
	private boolean isDelete = false ;
	
	// 构造函数
	public MyFavoritesLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.common, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setText("Edit");
		rightButton.setOnClickListener(this);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("My Favorites");
		loadingLayout = (RelativeLayout) baseView
		.findViewById(R.id.loadingLayout);
		
		bodyList.setSelector(R.drawable.details_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new FavoriteListAdapter(context, bodyList);
		bodyList.setAdapter(listAdapter);
		FavoriteListAsync favoriteListAsync = new FavoriteListAsync(context);
		favoriteListAsync.execute((Void)null);
	}

	
	public void reLoad(){
		FavoriteListAsync favoriteListAsync = new FavoriteListAsync(context);
		favoriteListAsync.execute((Void)null);
	}
	
	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			GoodsDetailLayout goodsDetailLayout = new GoodsDetailLayout(
					context, viewFlipper,"Back", listAdapter.getItem(arg2).getGoodsInfo());
			viewFlipper.addView(goodsDetailLayout);
			viewFlipper.showNext();
		}
	};

	class FavoriteListAsync extends
			AsyncTask<Void, Integer, ArrayList<CollectGoods>> {
		ProgressDialog pdialog;
		public FavoriteListAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected ArrayList<CollectGoods> doInBackground(Void... params) {
				return WebServiceClient.getMyCollectGoods(Constants.loginResponse.getUserId(), Constants.loginResponse.getUserToken());
		}
		@Override
		protected void onPostExecute(ArrayList<CollectGoods> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
//			pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			if(isDelete){
				isDelete = false ;
			}else{
				isDelete = true;
			}
			if(isDelete){
				rightButton.setText("Detele");
			}else{
				rightButton.setText("Edit");
			}
			listAdapter.setDelete(isDelete);
			listAdapter.notifyDataSetChanged();
			bodyList.postInvalidate();
			break;
		default:
			break;
		}

	}
}
